package org.wjy.post.action;

import java.io.IOException;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.wjy.post.dao.GoodsDao;
import org.wjy.post.other.Json;
import org.wjy.post.service.GoodsService;
import org.wjy.post.vo.Goods;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;

/**     
 * 类名称：LoginAction   
 * 类描述：用户控制器的实现
 * 创建人：anan   
 * 创建时间：2012-12-21 下午11:17:36   
 * 修改人：anan  
 * 修改时间：2012-12-21 下午11:17:36   
 * 修改备注：   
 * @version        
 * */
@Namespace("/post")
//定义商品action
public class GoodsAction extends ActionSupport {
	private static final long serialVersionUID = -2266695172069461659L;
	@Resource(name="goodsService")
	private GoodsService goodsService;
	private boolean flag;
	private int id;
	private String goodsnum;
	private String goodsname;
	private String goodssource;
	private double purchaseprice;
	private double price;
	@Override
	public String toString() {
		return "GoodsAction [goodsService=" + goodsService  
				+ ", id=" + id + ", goodsnum=" + goodsnum + ", goodsname="
				+ goodsname + ", goodssource=" + goodssource
				+ ", purchaseprice=" + purchaseprice + ", price=" + price + "]";
	}
	public GoodsService getGoodsService() {
		return goodsService;
	}
	public void setGoodsService(GoodsService goodsService) {
		this.goodsService = goodsService;
	}
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getGoodsnum() {
		return goodsnum;
	}
	public void setGoodsnum(String goodsnum) {
		this.goodsnum = goodsnum;
	}
	public String getGoodsname() {
		return goodsname;
	}
	public void setGoodsname(String goodsname) {
		this.goodsname = goodsname;
	}
	public String getGoodssource() {
		return goodssource;
	}
	public void setGoodssource(String goodssource) {
		this.goodssource = goodssource;
	}
	public double getPurchaseprice() {
		return purchaseprice;
	}
	public void setPurchaseprice(double purchaseprice) {
		this.purchaseprice = purchaseprice;
	}
	public double getPrice() {
		return price;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	
	public Goods getGoods(){
		Goods goods=new Goods();
		goods.setId(id);
		goods.setPurchaseprice(purchaseprice);
		goods.setPrice(price);
		goods.setGoodssource(goodssource);
		goods.setGoodsnum(goodsnum);
		goods.setGoodsname(goodsname);
		return goods;
	}
	
	@Action(value = "addgoodsAction", results = { })
	public void addgoods(){
		Json ret=new Json();
		ret.setMsg("添加失败!!!");
		Goods good=getGoods();
		flag=goodsService.addGoods(good);
		if(flag){
			ret.setSuccess(true);
			ret.setMsg("添加成功!!!");
		}
		String json = JSON.toJSONString(ret);
		try {
			ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
			ServletActionContext.getResponse().getWriter().write(json);
			ServletActionContext.getResponse().getWriter().flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	@Action(value = "delgoodsAction", results = { })
	public void delGoods(){
		
		Json ret=new Json();
		ret.setMsg("删除失败!!!");
		flag=goodsService.delGoods(this.id);
		if(flag){
			ret.setSuccess(true);
			ret.setMsg("删除成功!!!");
		}
		String json = JSON.toJSONString(ret);
		try {
			ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
			ServletActionContext.getResponse().getWriter().write(json);
			ServletActionContext.getResponse().getWriter().flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	@Action(value = "updategoodsAction", results = { })
	public void updateGoods(){
		Json ret=new Json();
		ret.setMsg("更新失败!!!");
		Goods good=getGoods();
		flag=goodsService.updateGoods(good);
		if(flag){
			ret.setSuccess(true);
			ret.setMsg("更新成功!!!");
		}
		String json = JSON.toJSONString(ret);
		try {
			ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
			ServletActionContext.getResponse().getWriter().write(json);
			ServletActionContext.getResponse().getWriter().flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	
	
	
}
